package com.zhy.deploy.executor;

import cn.hutool.core.util.StrUtil;
import com.zhy.deploy.config.*;
import com.zhy.deploy.executor.defaultImpl.DefaultCopyExecutor;
import com.zhy.deploy.executor.defaultImpl.DefaultGitExecutor;
import com.zhy.deploy.executor.defaultImpl.DefaultMavenExecutor;
import com.zhy.deploy.websocket.SessionContext;
import com.zhy.deploy.websocket.SocketMessage;
import org.apache.maven.shared.invoker.InvokerLogger;

import javax.websocket.Session;
import java.io.File;

public class Deploy {

    /**
     *
     * @param gitRepoURL 远程仓库地址
     * @param webappPath web部署目录
     * @throws Exception
     */
    public void deploy(String gitRepoURL, String webappPath) throws Exception {
        deploy(gitRepoURL, null,null,webappPath);
    }

    /**
     *
     * @param gitRepoURL  远程仓库地址
     * @param username  远程仓库用户登录名
     * @param password  远程仓库登陆密码
     * @param webappPath web部署目录
     * @throws Exception
     */
    public void deploy(String gitRepoURL,String username, String password, String webappPath) throws Exception {
        deploy(null, gitRepoURL,username,password, webappPath);
    }

    /**
     *
     * @param subProjectName 子项目名称
     * @param gitRepoURL  远程仓库地址
     * @param username  远程仓库用户登录名
     * @param password  远程仓库登陆密码
     * @param webappPath  web部署目录
     * @throws Exception
     */
    public void deploy(String subProjectName, String gitRepoURL,String username, String password, String webappPath) throws Exception {
        deploy(null, subProjectName, gitRepoURL, username, password, webappPath);
    }

    /**
     *
     * @param sessionId  HttpSession ID
     * @param subProjectName 子项目名称
     * @param gitRepoURL  远程仓库地址
     * @param username  远程仓库用户登录名
     * @param password  远程仓库登陆密码
     * @param webappPath web部署目录
     * @throws Exception
     */
    public void deploy(String sessionId, String subProjectName, String gitRepoURL, String username, String password, String webappPath) throws Exception {
        Session socketSession = SessionContext.getInstance().getSession(sessionId);
        SocketMessage message = new SocketMessage(socketSession, InvokerLogger.DEBUG);
        deploy(subProjectName, message, gitRepoURL, username, password, webappPath);
    }

    /**
     *
     * @param subProjectName 子项目名称
     * @param message  日志打印组件
     * @param gitRepoURL 远程仓库地址
     * @param username  远程仓库用户登录名
     * @param password  远程仓库登陆密码
     * @param webappPath web部署目录
     * @throws Exception
     */
    public void deploy(String subProjectName, SocketMessage message, String gitRepoURL, String username, String password, String webappPath) throws Exception {
        GitConfig gitConfig = new GitConfig();
        gitConfig.setGitRepoURL(gitRepoURL);
        gitConfig.setUsername(username);
        gitConfig.setPassword(password);

        MavenConfig mavenConfig = new MavenConfig();
        TomcatConfig tomcatConfig = new TomcatConfig();
        tomcatConfig.setWebappPath(webappPath);

        deploy(null,subProjectName,null, message, gitConfig, mavenConfig, tomcatConfig);
    }


    public void deploy(String projectName, String subProjectName, String workspace, SocketMessage message, GitConfig gitConfig, MavenConfig mavenConfig, TomcatConfig tomcatConfig) throws Exception {

        if(StrUtil.isEmpty(projectName)){
            String gitRepoURL = gitConfig.getGitRepoURL();
            projectName = gitRepoURL.substring(gitRepoURL.lastIndexOf("/") + 1, gitRepoURL.lastIndexOf("."));
        }
        if(StrUtil.isEmpty(workspace)){
            workspace = defaultWorkspace();
        }

        BuildMetadataBuilder builder = new BuildMetadataBuilder();

        BuildMetadata data = builder.setGitConfig(gitConfig)
                .setMavenConfig(mavenConfig)
                .setTomcatConfig(tomcatConfig)
                .setWorkspace(workspace)
                .setProjectName(projectName)
                .setSubProjectName(subProjectName)
                .message(message)
                .build();
        deploy(data);
    }

    private void deploy(BuildMetadata data) throws Exception {
        new DefaultGitExecutor(data).execute();

        new DefaultMavenExecutor(data).execute();

        new DefaultCopyExecutor(data).execute();
    }

    private String defaultWorkspace() {
        String home = System.getenv("HOME");
        File workspace = new File(home, ".deploy");
        if (!workspace.exists()) {
            workspace.mkdirs();
        }
        return workspace.getPath();
    }
}
